package com.lc.servlet;

import com.lc.bean.Userinfo;
import com.lc.service.UserinfoService;
import com.lc.service.impl.UserinfoServiceImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * @description:
 * @author: ManolinCoder
 * @time: 2021/8/20
 */
@WebServlet("/message")
public class messageServlet extends HttpServlet {
    UserinfoService userinfoService = new UserinfoServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uname = req.getParameter("uname");
        ArrayList<Userinfo> userinfos = (ArrayList<Userinfo>) userinfoService.getUserinfoByUname(uname);
        if (userinfos != null){
            Userinfo userinfo = new Userinfo();
            for (Userinfo user:userinfos) {
                userinfo = user;
            }
            //将users对象响应给客户端，使⽤PrintWriter来实现
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            //将对象转换成json格式
            JSONObject jsonObject = JSONObject.fromObject(userinfo);
            writer.print(jsonObject);
        }
    }
}
